package com.myapi.ModelPoker;
import java.io.Serializable;
import java.util.*;

import com.google.gwt.user.client.rpc.IsSerializable;
import com.myapi.client.DAO.CardDAO;
import com.myapi.client.DAO.CardDeckDAO;



/**
 * CarDeck
 * @author Realease by Yoan Olivier
 *
 */
public class CardDeck implements IsSerializable{
	

	   
	public static final int DECKSIZE = 52;
	
	/**
	 * @gwt.typeArgs<com.myapi.client.Card>
	 */
	private ArrayList<Card> cards;

	/**
	 * Constructor CardDeck
	 * 
	 */

	public CardDeck() {
		
		cards = new ArrayList<Card>(DECKSIZE);
		//fill the deck specified by the cards array with a valid deck of cards
		for(int i = 2; i <= 14; i++) {
			Card spadeCard = new Card(i, Card.SPADE);
			Card heartCard = new Card(i, Card.HEART);
			Card clubCard = new Card(i, Card.CLUB);
			Card diamondCard = new Card(i, Card.DIAMOND);
			cards.add(spadeCard);
			cards.add(heartCard);
			cards.add(clubCard);
			cards.add(diamondCard);
		}
	}


	public Card getRandomCard(){
		return cards.get((int) (Math.random()*cards.size()));
	}
	/**
	 * Could have been implemented as a stack, but chose to "Do the simplest thing that could possibly work"
	 * @return Card on the top of the deck
	 */
	public Card getTopCard() {
		if(cards.size() > 0) {
			return cards.remove(0);
		}
		else {
			return null;
		}
	}
/**
 * Get the number of cards
 * @return numbers of cards
 */
	public int getNumCards() {
		return cards.size();
	}

	/**
	 * randomise order of deck
	 */
	public void shuffleDeck() {
		/**
		 * * @gwt.typeArgs <com.myapi.client.Card>
		 */
		Collections.shuffle(cards);
	}


	public CardDeckDAO getInfo() {
		CardDeckDAO cardDeckDAO=new CardDeckDAO();
		ArrayList<CardDAO> cards=new ArrayList<CardDAO>();
		for(int i=0;i<this.getNumCards();i++){
			cards.add(this.cards.get(i).getInfo());
		
		}
		cardDeckDAO.setCards(cards);
		return cardDeckDAO;
	}
	
}
